﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EADCA3MVCApp.Models;

namespace EADCA3MVCApp.Controllers
{ 
    public class CloudController : Controller
    {
        private CloudEntities1 db = new CloudEntities1();

       [HttpGet]
        public ActionResult Services()
        {  
            ViewBag.PageID = "Services";
            var cloudServices = db.CloudServices.ToList();

            return View(cloudServices);
        }
        
        //
        // GET: /Cloud/

        public ViewResult Index()
        {
            LogOn p = new LogOn();
            ViewBag.PageID = "Index";
            return View(p);
        }
        
        [HttpPost]
        public ActionResult Index(LogOn p)
        {
            if (ModelState.IsValid)
            {
                LogOn password = new LogOn("cloud");

                if (password.Password == p.Password)
                {
                    ViewBag.PageID = "Index";
                    var cloudServices = db.CloudServices.ToList();

                    return View("IndexLogOn", cloudServices);
                }
                else
                {
                    ViewBag.PageID = "Services";
                    return RedirectToAction("Services");
                }
            }
            ViewBag.PageID = "Services";
            return RedirectToAction("Services");
        }

        public ViewResult IndexLogOn()
        {
            ViewBag.PageID = "Index";
            var cloudServices = db.CloudServices.ToList();

            return View("IndexLogOn", cloudServices);
        }

        //
        // GET: /Cloud/Details/5

        public ViewResult Details(int id)
        {
            ViewBag.PageID = "Index";
            CloudService cloudservice = db.CloudServices.Single(c => c.ID == id);
            return View(cloudservice);
        }

        //
        // GET: /Cloud/Create

        public ActionResult Create()
        {
            ViewBag.PageID = "Index";
            ViewBag.InstanceSize = new SelectList(CloudService.Sizes);
            return View();
        } 

        //
        // POST: /Cloud/Create

        [HttpPost]
        public ActionResult Create(CloudService cloudservice)
        {
            if (ModelState.IsValid)
            {
                db.CloudServices.AddObject(cloudservice);
                db.SaveChanges();
                return RedirectToAction("IndexLogOn");  
            }

            ViewBag.PageID = "Index";
            return View(cloudservice);
        }
        
        //
        // GET: /Cloud/Edit/5
 
        public ActionResult Edit(int id)
        {
            CloudService cloudservice = db.CloudServices.Single(c => c.ID == id);
            ViewBag.PageID = "Index";
            return View(cloudservice);
        }

        //
        // POST: /Cloud/Edit/5

        [HttpPost]
        public ActionResult Edit(CloudService cloudservice)
        {
            if (ModelState.IsValid)
            {
                db.CloudServices.Attach(cloudservice);
                db.ObjectStateManager.ChangeObjectState(cloudservice, EntityState.Modified);
                db.SaveChanges();
                ViewBag.PageID = "Index";
                return RedirectToAction("IndexLogOn");
            }
            ViewBag.PageID = "IndexLogOn";
            return View(cloudservice);
        }

        //
        // GET: /Cloud/Delete/5
 
        public ActionResult Delete(int id)
        {
            CloudService cloudservice = db.CloudServices.Single(c => c.ID == id);
            ViewBag.PageID = "Index";
            return View(cloudservice);
        }

        //
        // POST: /Cloud/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            CloudService cloudservice = db.CloudServices.Single(c => c.ID == id);
            db.CloudServices.DeleteObject(cloudservice);
            db.SaveChanges();
            ViewBag.PageID = "Index";
            return RedirectToAction("IndexLogOn");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        //  GET: /Cloud/ExistingServer/

        public ActionResult ExistingServer()
        {
            ExistingServer es = new ExistingServer() { ServerCost = 399.00, MaintainanceCost = 25.00, SoftwareLicenceCost = 24.00, NoOfServers = 2 };
            ViewBag.PageID = "ExistingServer";
            return View(es);
        }

        //
        //  POST: /Cloud/ExistingServer/

        [HttpPost]
        public ActionResult ExistingServer(ExistingServer es)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("ShowTotalCost", es);
            }
            ViewBag.PageID = "ExistingServer";
            return View();
        }

        //
        // GET:  /Cloud/ShowTotalCost/

        public ActionResult ShowTotalCost(ExistingServer es)
        {
            ViewBag.PageID = "ExistingServer";
            return View(es);
        }

        //
        // POST: /Cloud/ShowTotalCost/

        [HttpPost, ActionName("ShowTotalCost")]
        public ActionResult ShowTotalCostConfirmed(ExistingServer es)
        {
            ViewBag.PageID = "ExistingServer";
            return RedirectToAction("CalculateSaving", es);
        }
        
        //
        // GET: /Cloud/CalculateSaving/
        public ActionResult CalculateSaving(ExistingServer es)
        {
            CloudService requiredService = new CloudService();
            // get the cheapest service for this given insance size
            requiredService = requiredService.GetCheapestService(es.InstanceSizeRequired);
            //this method sets the Total Cost of Cloud Service
            requiredService.GetTotalCost(es.NoOfServers);
           
            // get the actual saving (Total Cloud Monthly cost - Existing Server monthly cost)
            double saving = es.MonthlyCost - requiredService.TotalCost;

            // set Viewbag for saving, provider, Total cloud service cost and Cloud Instance size
            if (saving <= 0)
            {
                ViewBag.Saving = "Woops! : we can't find any saving for you, the cheapest offer is shown below";
            }
            else
            {
                ViewBag.Saving = "€" + saving.ToString("0.00") + " per month, moving to " + requiredService.Company + "'s Cloud";
            }
            ViewBag.TotalCost = requiredService.TotalCost.ToString("0.00");
            ViewBag.InstanceSize = requiredService.InstanceSize;
            ViewBag.Provider = requiredService.Company;

            // get a list of services available for the instance size required
            var services = requiredService.GetApplicableService(es.InstanceSizeRequired);
            ViewBag.PageID = "ExistingServer";
            return View(services);
         }
        
     }
}